/**
 * vuex的mutations模块
 */
import {RECEIVE_ADDRESS,RECEIVE_CATEGORYS,RECEVIE_SHOPS,RECEVIE_USER_INFO,RESET_USER_INFO,RECEIVE_GOODS,RECEIVE_RATINGS,RECEIVE_INFO,INCREMENT_FOOD_COUNT,DECREMENT_FOOD_COUNT,CLEAR_CART,RECEIVE_SEARCH_SHOPS} from './mutation-types'
import Vue from 'vue'
export default{
    [RECEIVE_ADDRESS](state,{address}){ //参数是一个对象{}==payload
        state.address = address; //对象
    },
    [RECEIVE_CATEGORYS](state,{categorys}){
        state.categorys = categorys; //食品分类数组
    },
    [RECEVIE_SHOPS](state,{shops}){
        state.shops = shops; //商家列表数组
    },
    [RECEVIE_USER_INFO](state,{userInfo}){ //用户信息
        state.userInfo = userInfo;
    },
    [RESET_USER_INFO](state){ //重置用户名信息
        state.userInfo = {};
    },
    [RECEIVE_GOODS](state,{goods}){
        state.goods = goods;
    },
    [RECEIVE_RATINGS](state,{ratings}){
        state.ratings = ratings;
    },
    [RECEIVE_INFO](state,{info}){
        state.info = info;
    },
    [INCREMENT_FOOD_COUNT](state,{food}){
        if(!food.count){ //第一次增加
            //Vue.set( target, propertyName/index, value )
            //food.count = 1; //新增属性没有数据绑定效果
            Vue.set(food,'count',1); //Vue.set()方法将响应式属性添加到嵌套的对象上
            //将food添加到cartFoods中去
            state.cartFoods.push(food);
        }else{
            food.count++;
        }
    },
    [DECREMENT_FOOD_COUNT](state,{food}){
        if(food.count){ //有值再去减
            food.count--;
            if(food.count===0){
                //将food从cartFoods中删除
                state.cartFoods.splice(state.cartFoods.indexOf(food),1);
            }
        }
    },
    [CLEAR_CART](state){
        //清楚food中的count
        state.cartFoods.forEach(food => food.count=0);
        //移除购物车中所有购物项
        state.cartFoods = [];
    },
    [RECEIVE_SEARCH_SHOPS](state,{searchShops}){
        state.searchShops = searchShops;
    }
}